<?php

require_once ("MY_SecuredController.php");

class MY_Controller extends Controller {
	public function MY_Controller() {
		parent::Controller();
		log_message('debug', "MY_Controller Class Initialized");

		 
		//$this->output->enable_profiler(TRUE);
	}

	public function render($view, $viewData = null, $message = null) {
		$this->_preRenderOutputHeaders();

		$data = $this->_preRenderAddNavigation();

		// copy the array data passed to our own variable before rendering
		if (is_array($viewData)) {
			foreach ($viewData as $key => $value) {
				$data[$key] = $value;
			}
				
			unset($viewData);
		}

		if ($message)
		$data['messages'] = $message;

		// if there are pending messages... overwrite?
		$flashdata = $this->session->flashdata('message');

		if ($flashdata)
		$data['messages'] = $this->session->flashdata('message');
			
		$this->load->view($view, $data);
	}

	protected function _preRenderOutputHeaders() {
		$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	}

	protected function _preRenderAddNavigation() {
		$user = Current_User::user();

		$navdata['authenticated'] = $user != null;
		$navdata['user'] = $user != null? $user->name : null;
		$navdata['userid'] = $user != null? $user->id : null;

		//$data['navigation'] = $this->load->view('menu/navigation', $navdata, TRUE);
		//$data['profileMenu'] = $this->load->view('menu/profile', $navdata, TRUE);
		$data['userid'] = $user != null? $user->id : null;

		return $data;
	}

	public function info($message) {
		$data['msg'] = $message;
		return $this->load->view('msgs/info', $data, TRUE);
	}

	public function error($message) {
		log_message('debug', '>>> MY_Controller:error message = '.$message);

		if (is_array($message)) {
			$data['msg'] = $message['message'];
			$data['exception'] = $message['exception'];
		}
		else {
			if (stripos($message, '<li>') === false) {
				$data['msg'] = '<li>'.$message.'</li>';
			}
			else {
				$data['msg'] = $message;
			}
		}

		return $this->load->view('msgs/error', $data, TRUE);
	}

	public function redirect($where, $message, $isError = FALSE) {
		if ($message) {
			$this->session->set_flashdata('message', $isError? $this->error($message) : $this->info($message));
		}

		redirect($where);
	}

	protected function _loadValidationMessages() {
		$this->lang->load('form_validation');
		
		$this->form_validation->set_message('required',lang('validation.required'));
		$this->form_validation->set_message('numeric', lang('validation.numeric'));
		$this->form_validation->set_message('min_length', lang('validation.min_length'));
		$this->form_validation->set_message('max_length', lang('validation.max_length'));
		$this->form_validation->set_message('unique', lang('validation.unique'));
		$this->form_validation->set_message('matches', lang('validation.matches'));
		$this->form_validation->set_message('xss_clean', lang('validation.xss_clean'));
		$this->form_validation->set_message('valid_email', lang('validationvalid_email'));
		
		$this->form_validation->set_message('authenticate',lang('validation.invalid_login'));
	}
}
?>